Executors, CountDownLatch, CyclicBarrier, এবং Semaphore এর ব্যবহার

Java Technologies - জাভা কনকারেন্সি (Java Concurrency) - Concurrency Utilities (java.util.concurrent)
277

জাভার কনকারেন্সি টুলকিটে এই ক্লাসগুলো ব্যবহারের মাধ্যমে মাল্টিথ্রেডিং প্রোগ্রাম সহজে পরিচালনা করা যায়। এখানে Executors, CountDownLatch, CyclicBarrier, এবং Semaphore এর ব্যবহার বিস্তারিতভাবে ব্যাখ্যা করা হলো।


১. Executors

Executors হলো একটি থ্রেড পুল তৈরি ও পরিচালনার জন্য জাভার একটি ক্লাস। এটি একাধিক থ্রেড তৈরি ও পরিচালনা করার কাজ সহজ করে।

ব্যবহার:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorsExample {
    public static void main(String[] args) {
        // একটি Fixed Thread Pool তৈরি
        ExecutorService executor = Executors.newFixedThreadPool(3);

        // কাজ জমা দিন
        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            executor.submit(() -> {
                System.out.println("Task " + taskId + " is running in thread " + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000); // কাজ করছে
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        // থ্রেড পুল বন্ধ করুন
        executor.shutdown();
    }
}

আউটপুট (উদাহরণ):

Task 1 is running in thread pool-1-thread-1
Task 2 is running in thread pool-1-thread-2
Task 3 is running in thread pool-1-thread-3
Task 4 is running in thread pool-1-thread-1
Task 5 is running in thread pool-1-thread-2

২. CountDownLatch

CountDownLatch একাধিক থ্রেডকে সমন্বয় করতে ব্যবহার করা হয়। এটি থ্রেডগুলোকে নির্দিষ্ট সংখ্যক অপারেশন শেষ না হওয়া পর্যন্ত অপেক্ষা করায়।

ব্যবহার:

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3); // ৩টি কাজ শেষ হওয়া পর্যন্ত অপেক্ষা করবে

        for (int i = 1; i <= 3; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " completed its task.");
                latch.countDown(); // কাউন্টডাউন
            }).start();
        }

        latch.await(); // মেইন থ্রেড অপেক্ষা করবে
        System.out.println("All tasks are completed. Main thread is proceeding.");
    }
}

আউটপুট:

Thread-1 completed its task.
Thread-2 completed its task.
Thread-3 completed its task.
All tasks are completed. Main thread is proceeding.

৩. CyclicBarrier

CyclicBarrier হলো একটি টুল, যা একাধিক থ্রেডকে নির্দিষ্ট পয়েন্টে অপেক্ষা করায় যতক্ষণ না সব থ্রেড পৌঁছে। এটি পুনরায় ব্যবহারযোগ্য।

ব্যবহার:

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierExample {
    public static void main(String[] args) {
        int numberOfThreads = 3;
        CyclicBarrier barrier = new CyclicBarrier(numberOfThreads, () -> {
            System.out.println("All threads reached the barrier. Proceeding...");
        });

        for (int i = 1; i <= numberOfThreads; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " reached the barrier.");
                try {
                    barrier.await(); // ব্যারিয়ারে অপেক্ষা
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

আউটপুট:

Thread-1 reached the barrier.
Thread-2 reached the barrier.
Thread-3 reached the barrier.
All threads reached the barrier. Proceeding...

৪. Semaphore

Semaphore হলো একটি টুল, যা নির্দিষ্ট সংখ্যক থ্রেডকে একবারে একটি রিসোর্স অ্যাক্সেস করার অনুমতি দেয়।

ব্যবহার:

import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2); // ২টি থ্রেড একবারে রিসোর্স অ্যাক্সেস করতে পারবে

        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            new Thread(() -> {
                try {
                    System.out.println("Task " + taskId + " is waiting for a permit.");
                    semaphore.acquire(); // পারমিট নেওয়া
                    System.out.println("Task " + taskId + " got a permit and is working.");
                    Thread.sleep(2000); // কাজ করছে
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("Task " + taskId + " released the permit.");
                    semaphore.release(); // পারমিট ছাড়া
                }
            }).start();
        }
    }
}

আউটপুট:

Task 1 is waiting for a permit.
Task 2 is waiting for a permit.
Task 1 got a permit and is working.
Task 2 got a permit and is working.
Task 3 is waiting for a permit.
Task 4 is waiting for a permit.
Task 5 is waiting for a permit.
Task 1 released the permit.
Task 3 got a permit and is working.
Task 2 released the permit.
Task 4 got a permit and is working.

একত্রে ব্যবহারের উদাহরণ

import java.util.concurrent.*;

public class ConcurrencyExample {
    public static void main(String[] args) throws InterruptedException {
        int numberOfThreads = 3;

        ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
        CountDownLatch latch = new CountDownLatch(numberOfThreads);
        CyclicBarrier barrier = new CyclicBarrier(numberOfThreads);
        Semaphore semaphore = new Semaphore(2);

        for (int i = 1; i <= numberOfThreads; i++) {
            final int taskId = i;
            executor.submit(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("Task " + taskId + " acquired semaphore.");
                    barrier.await();
                    System.out.println("Task " + taskId + " crossed the barrier.");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                    latch.countDown();
                }
            });
        }

        latch.await();
        executor.shutdown();
        System.out.println("All tasks completed. Main thread proceeding.");
    }
}

আউটপুট:

Task 1 acquired semaphore.
Task 2 acquired semaphore.
Task 3 acquired semaphore.
Task 1 crossed the barrier.
Task 2 crossed the barrier.
Task 3 crossed the barrier.
All tasks completed. Main thread proceeding.

  1. Executors: থ্রেড ম্যানেজমেন্ট সহজ করে।
  2. CountDownLatch: থ্রেডগুলোর কাজ শেষ হওয়া পর্যন্ত মেইন থ্রেড অপেক্ষা করে।
  3. CyclicBarrier: থ্রেডগুলোকে একত্রে কাজ শুরু করার জন্য সিঙ্ক্রোনাইজ করে।
  4. Semaphore: নির্দিষ্ট সংখ্যক থ্রেডকে একবারে রিসোর্স অ্যাক্সেস করতে দেয়।

এই ক্লাসগুলো একত্রে ব্যবহার করে জটিল মাল্টিথ্রেডেড অ্যাপ্লিকেশন সহজে তৈরি করা যায়।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...